layui.define([ 'form', 'table','userApi' ], function(exports) {
	var form = layui.form;
	var table = layui.table;
	var userApi=layui.userApi;
	var userTable = null;
	
	var view ={
		init:function(){
			this.initTable();
			this.initSearchForm();
			this.initToolBar();
			window.dataReload = function(){
				Lib.doSearchForm($("#searchForm"),userTable)
			}
		},
		initTable:function(){
			userTable = table.render({
				elem : '#userTable',
				height : Lib.getTableHeight(1),
                cellMinWidth: 100,
				method : 'post',
				url : Common.ctxPath + '/admin/user/list.json' //数据接口
				,page : true //开启分页
				,limit : 10,
				cols : [ [ //表头
					{type : 'checkbox',fixed:'left'},
                    {title : '序号',fixed:'left',toolbar : '#indexTpl',type:'numbers', width : 60 },
					//{field : 'id',title : 'ID',width : 80,fixed:'left',calign:'center'},
					{field : 'code',title : '帐号',align : 'center',},
					{field : 'userName',title : '姓名',align : 'center',},
					{field : 'orgName',title : '所在事业部',align : 'center',},
					{field : 'statusText',title : '状态',width : 120,align : 'center',},
					{field : 'createTime',title : '创建时间',width : 160,sort : true,align : 'center',
						templet:function(d){
						return Common.getDate(d.createTime,'yyyy-MM-dd HH:mm:ss');
						},
					},
                    {fixed : 'right', title : '操作', align : 'left', toolbar : '#toobar', width:280 }

				] ]
			});
		},
		
		initSearchForm:function(){
			Lib.initSearchForm( $("#searchForm"),userTable,form);
		},
		initToolBar:function(){
			toolbar = {
					add : function() { //获取选中数据
						var url = "/admin/user/add.do";
						Common.openDlg0(url,'450px','455px',"用户管理>新增");
					},
                	batchDel : function() {
						layui.use(['del'], function(){
							  var delView = layui.del
							  delView.delBatch();
						});
					},
					exportUsers:function(){
					    Common.openConfirm("确认要导出这些用户?",function(){
					        userApi.exportUsers($("#searchForm"),function(fileId){
					           Lib.download(fileId);
					        })
		                })
                    }
				};
			$('.ext-toolbar').on('click', function() {
				var type = $(this).data('type');
				toolbar[type] ? toolbar[type].call(this) : '';
			});
		}
	};
	//监听工具条
    table.on('tool(userTable)', function(obj) {
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent === 'update'){
            var url = "/admin/user/edit.do?id="+data.id;
            Common.openDlg0(url,'450px','455px',"用户管理>编辑");

        }else if(layEvent === 'del'){
            layui.use(['del'], function(){
                var delView = layui.del;
                delView.delSingle(data);
            });
        }else if(layEvent === 'untying'){
            Common.openConfirm("确认要解绑手机串号吗?",function(){
                var url =Common.ctxPath + "/admin/user/untyingPhone.json";
                Common.post(url,"ucode="+data.code,function(rsp){
                    Common.success("解绑成功！");
                    dataReload();
                });
            });
        }
        else if(layEvent === 'userRole'){
            var url = "/admin/user/role/list.do?id="+data.id;
            Common.openDlg50(url,"用户管理>"+data.userName+">角色管理");
		}
        else if(layEvent === 'updatePwd'){
            var url = "/admin/user/changePassword.do?id="+data.id;
            Common.openDlg0(url,'400px','455px',"用户管理>更改密码");
        }
    });
	 exports('index',view);
	
});